package com.atguigu.gulimall.test.thread;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : 刘思盖
 * @Date : 2021/9/1 14:31
 */
public class SynchronizeModel {
    public static Object lock = new Object();
    public List<String> list = new ArrayList<>();

    class Producer{
        SynchronizeModel synchronizeModel = new SynchronizeModel();
        public void pruduct(){
            synchronized (lock){
                // 如果生产者 成产了10个，则将该线程进行挂起，并释放CUP使用权
                while(list.size()==10){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.add("元素");
                lock.notifyAll();
            }
        }
    }
    class Consumer{
        SynchronizeModel synchronizeModel = new SynchronizeModel();
        public void consume(){
            synchronized (lock){
                while(list.size()==0){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}

